package org.kjtc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.kjtc.entity.AbnormalRule;
import org.kjtc.service.AbnormalRuleService;
import org.kjtc.service.ActionHistoryService;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class AbnormalRuleController extends BaseController{

	@Resource(name = "abnormalRuleService")
	private AbnormalRuleService abnormalRuleService;

	@Resource(name = "actionHistoryService")
	private ActionHistoryService actionHistoryService;

	@RequestMapping(value = "/searchAbnormalRule")
	@ResponseBody
	public Map<String, Object> searchAbnormalRule(AbnormalRule abnormalRule){

		logger.info("异常规则设置查询开始");
		Map<String, Object> map = new HashMap<String, Object>();

		// 获取所有设备数据
		List<AbnormalRule> list = null;
		try {
			list = abnormalRuleService.searchAbnormalRule(abnormalRule);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("异常规则设置查询异常" + e.getMessage());
			return map;
		}
		map.put("abnormalRuleList", list);

		logger.info("异常规则设置查询结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "异常规则设置查询");
		abnormalRule = null;
		return map;
	}

	@ResponseBody
	@RequestMapping(value = "/saveAbnormalRule")
	public Map<String, Object> saveAbnormalRule(AbnormalRule abnormalRule){

		logger.info("异常规则设置添加开始");
		Map<String, Object> map = new HashMap<String, Object>();
		abnormalRule.setCreateUser(this.getUesr().getUserID());
		abnormalRule.setUpdateUser(this.getUesr().getUserID());
		int count = 0;
		try {
			count = abnormalRuleService.insertAbnormalRule(abnormalRule);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (DuplicateKeyException ex) {
			String[] error = ex.getCause().getMessage().split("'");
			if ("PRIMARY".equals(error[3].trim())) {
				map.put("result", "AbnormalIDExist");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error("异常规则设置添加异常");
		}
		logger.info("异常规则设置添加结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "异常规则设置添加");
		abnormalRule = null;
		return map;
	}

	@RequestMapping(value = "/updateAbnormalRule")
	@ResponseBody
	public Map<String, Object> updateAbnormalRule(AbnormalRule abnormalRule) {
		logger.info("异常规则设置修改开始");
		Map<String, Object> map = new HashMap<String, Object>();
		abnormalRule.setUpdateUser(this.getUesr().getUserID());
		int count = 0;
		try {
			count = abnormalRuleService.updateAbnormalRule(abnormalRule);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		}  catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error("异常规则设置修改异常");
		}
		logger.info("异常规则设置修改结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "异常规则设置修改");
		abnormalRule = null;
		return map;
	}

	@RequestMapping(value = "/deleteAbnormalRule")
	@ResponseBody
	public Object deleteAbnormalRule(String abnormalID) {

		logger.info("异常规则设置删除开始");
		Map<String, Object> map = new HashMap<String, Object>();
		String[] IDList = abnormalID.split(",");
		AbnormalRule abnormalRule = new AbnormalRule();
		abnormalRule.setAbnormalIDList(IDList);
		abnormalRule.setUpdateUser(this.getUesr().getUserID());
		int count = 0;
		try {
			count = abnormalRuleService.deleteAbnormalRule(abnormalRule);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error("异常规则设置删除异常:" + e.getMessage());
		}
		logger.info("异常规则设置删除结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "异常规则设置删除");
		abnormalRule = null;
		return map;
	}
}
